Saturate.onUpperBound

Implements saturation for operators +=, -=, *=, /=, %=, ^^=, &=, |=, ^=, <<=, >>=, and >>>=. This hook is called if the result of the binary operation does not fit in Lhs without loss of information or a change in sign.

  1. T onLowerBound(Rhs rhs, T bound)
  2. T onUpperBound(Rhs rhs, T bound)
    struct Saturate
    static
    T
    onUpperBound
    (
    Rhs
    T
    )
    (
    Rhs rhs
    ,
    )

Parameters

Rhs

The right-hand side type in the assignment, after the operation has been computed

bound
Type: T

The bound being violated

Return Value

Type: T

Lhs.max if rhs >= 0, Lhs.min otherwise.

Examples

1 auto x = checked!Saturate(short(100));
2 x += 33000;
3 assert(x == short.max);
4 x -= 70000;
5 assert(x == short.min);

Meta